***************************
*use *** "Conceptualizing and Measuring Subnational Democracy Across Indian States_raw.dta"

use "C:\Users\iharber1\Dropbox\ERC - Imke\India Paper\Database\Revisions 2018\Replication Materials 2019\Upload to Dataverse\Conceptualizing and Measuring Subnational Democracy Across Indian States_raw.dta", replace

*** generate election sequence and tsset
sort isostate_in year
by isostate_in : gen elec_seq = _n
encode isostate, gen (isostate_n)

tsset isostate_n elec_seq
tab elec_seq 


*** Transform ENPL into curvilinear variable with 5 as maximum of the curve

gen enpl5 = .
replace enpl5 = enpl/5
replace enpl5 = 1-enpl5
replace enpl5 = enpl5*enpl5
replace enpl5 = 1-enpl5
twoway (scatter enpl5 enpl)

* generate nbdeath logged
gen nbdeath_log = .
replace nbdeath_log = nbdeath + 1
sum nbdeath_log
replace nbdeath_log = ln(nbdeath_log)
twoway (scatter  nbdeath_log nbdeath)

**** Normalize variables between 0 and 1 to ensure equal weight in the index 

sum changeparty
gen n_changeparty = changeparty
	replace n_changeparty = (changeparty - `r(min)') / (`r(max)'-`r(min)')
	
sum enpl5
gen n_enpl5 = enpl5
	replace n_enpl5 = (enpl5 - `r(min)') / (`r(max)'-`r(min)')
	
sum sop
gen n_sop = sop
	replace n_sop = (sop - `r(min)') / (`r(max)'-`r(min)')
	
sum nbdeath_log
gen n_nbdeath_log = nbdeath_log
	replace n_nbdeath_log = (nbdeath_log - `r(min)') / (`r(max)'-`r(min)')
	replace n_nbdeath_log = 1-n_nbdeath_log
	
sum prc_consta
gen n_prc_consta = prc_consta
	replace n_prc_consta = (100-prc_consta)/100
	
sum n_changeparty n_enpl5 n_sop n_nbdeath_log n_prc_consta
	
*** Calculate normalized contestation

gen n_contestation5 = ( n_enpl5 + n_sop)/2
sum n_contestation5


*** Calculate clean elections dimension with log
gen n_cleanlog = ( n_nbdeath_log+ n_prc_consta)/2
sum n_cleanlog


*** Calculate small index (without President's Rule and Clean Elections; normalized)

gen n_small_index5 = changeparty + n_contestation5
sum n_small_index5


*** Add President's Rule 

*Reverse coding so 0 reflects PR 
gen pr2 = pr
replace pr = 1 if pr2 == 0
replace pr = 0 if pr2 == 1


* Calculate index 

gen index5l = n_small_index5 * pr * n_cleanlog 


* Calculate contestation and clean elections (without PR)

gen index5_cleanl = n_small_index5 * n_cleanlog 



********************************************************

**Calculate averages


*By state, whole period


by isostate_n, sort: egen mean_index5l = mean(index5l)


**************************************************************************
**************************************************************************

* Figure 2: Average by state, whole period

graph hbar (mean) mean_index5l, over(stateut, sort(mean_index5l) label(angle(horizontal) labsize(small))) bargap(2) ytitle(Electoral Democracy Index(Mean)) scheme(s1mono)

* Figure 3: Time trends 
 destring year, replace
 
 twoway (dropline index5l year, mcolor(black) msize(vsmall) lcolor(black)) (scatter n_small_index5 year, mcolor(gs5) msize(vlarge) msymbol(circle_hollow)) (scatter index5_cleanl year, mcolor(gs3) msize(medsmall) msymbol(triangle_hollow)), ylabel(, angle(horizontal)) xtitle(Year) xtitle(, size(vsmall)) legend(order(1 "Electoral Democracy Index" 2 "Turnover + Contestation" 3 "Turnover + Contestation * Clean Elections" ) rows(3) size(vsmall) box fcolor(%0) lcolor(%0)) scheme(s1manual) by(stateut)
 
 * Apply smoother to identify temporal trends 
 smooth 4253 index5l, generate (trends)
 
* Figure 4: Time trends 
 
 twoway (line trends year, lcolor(%80) lpattern(shortdash)), ylabel(, angle(horizontal)) xtitle(Year) xtitle(, size(vsmall)) scheme(s1manual) by(stateut)
 

 drop pr2
 
 rename index5l index
 
* save "Conceptualizing and Measuring Subnational Democracy Across Indian States.dta"
